<?php
/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/7/28
 * Time: 12:12
 */
namespace app\common\redis;

use app\common\library\CommonFun;
use app\common\model\article\PublishArticleModel;
use app\common\model\article\PublishWeiboModel;
use app\common\model\coin\CoinRuntimeModel;
use app\common\model\pair\PairRuntimeModel;
use app\common\model\user\UserCoinModel;
use app\common\model\user\UserPairModel;
use app\common\service\PythonApiService;

class WeiboSpiderRedis extends RedisModel
{
    /**
     * Redis配置名称
     * @var string
     */
    public $redis = 'default';

    /**
     * Redis key
     * @var string
     */
    public $key = 'list:weibo_spider';
    
    /**
     * 创建用户文章的列表
     * @param int $page 当前页数
     * @param int $user_id 用户编号
     * @param array $special_user_coin_id_list 指定用户持币列表 (主要用于非登录持币推荐)
     */
    public function CreateUserArticleListV2( $page, $user_id, $client_sn, array $special_user_coin_id_list=array() ) {
    	$modelUserPair  = UserPairModel::getInstance();
    	// Python接口模型
    	$servicePython = PythonApiService::getInstance();
    	// 每页数量
    	$page_size = 10;
    	if ($special_user_coin_id_list) {
    		$user_coin_id_list = $special_user_coin_id_list;
    	}else {
    		if ( $user_id ) {
    			// 用户持仓币列表
    			$user_asset_coin_id_list = $servicePython->getUserAssetCoinID($user_id);
    			// 用户关注币列表
    			$user_pair_coin_id_list = array();
    			$user_pair_id_list = $modelUserPair->get_user_pair_id_list($user_id);
    			if ( $user_pair_id_list ) {
    				$modePair = PairRuntimeModel::getInstance();
    				$pair_data = $modePair->muti_get_data_by_id_list($user_pair_id_list);
    				$user_pair_coin_id_list = array_column($pair_data, "base");
    			}
    			
    			// 用户所有币种编号列表
    			$user_coin_id_list = array_unique(array_merge($user_asset_coin_id_list, $user_pair_coin_id_list));
    			// 新用户没有持仓和关注
    			if ( !$user_coin_id_list ) {
    				$coinTokenModel = CoinRuntimeModel::getInstance();
    				$coinMap = array(
    						'type' => 1,
    						'status' => 1,
    						'deleted' => -1,
    						'sort' => array('gt', 0),
    				);
    				$user_coin_id_list = $coinTokenModel->search_id_list_no_page($coinMap, 'id', true, 'sort');
    			}
    		}else {
    			$coinTokenModel = CoinRuntimeModel::getInstance();
    			$coinMap = array(
    					'type' => 1,
    					'status' => 1,
    					'deleted' => -1,
    					'sort' => array('gt', 0),
    			);
    			$user_coin_id_list = $coinTokenModel->search_id_list_no_page($coinMap, 'id', true, 'sort');
    		}
    	}
    	
    	if ( !$user_coin_id_list ) {
    		return;
    	}
    	
    	// 币相关文章Redis
    	$coinArticleRedis = new CoinArticleRedis();
    	// 获取用户币相关文章数据
    	$user_coin_aw_list = [];
    	// 所有文章数据
    	$all_aw_list = [];
    	foreach ( $user_coin_id_list as $_coin_id ) {
    		$user_coin_aw_list[$_coin_id] = $coinArticleRedis->GetCoinAllTypeArticle($_coin_id);
    		$all_aw_list = array_merge($all_aw_list, $user_coin_aw_list[$_coin_id]);
    	}
    	$asset_aw_list = [];
    	if ( $all_aw_list ) {
    		$all_aw_list = array_values($all_aw_list);
    		$start = ($page-1)*$page_size;
    		$all_aw_list = array_slice($all_aw_list, $start, $page_size);
    		foreach ( $all_aw_list as $coin_id => $aw_id ) {
    			if ( isset($asset_aw_list[$aw_id]) ) {
    				continue;
    			}
    			$unique_key = CommonFun::create_unique_id();
    			list( $type, $id ) = explode("_", $aw_id);
    			$asset_aw_list[$aw_id] = array(
    					"unique_key" => $unique_key,
    					"type" => $type,
    					"id" => $id,
    					"coin_id" => $coin_id,
    					"label_name" => "",
    					"label_icon" => "",
    			);
    		}
    	}
    	
    	$hash_list = [];
    	
    	if ($asset_aw_list) $hash_list = $asset_aw_list;
    	
    	if ( $hash_list ) {
    		// 设置unique_key列表
    		$key = "UserArticleHashTable:{$user_id}:{$client_sn}";
    		$this->PipeStart();
    		$this->SetRedisKey($key);
    		if ( $this->ExistsKey() ) {
    			$this->Delete();
    		}
    		foreach ( $hash_list as $v ) {
    			$this->ListRpush(CommonFun::json_encode2($v));
    		}
    		$this->PipeExec();
    	}
    	
    	
    	
    }
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}